﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace Socket服务器
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Application_DispatcherUnhandledException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }
        /// <summary>
        /// 非WPF窗体异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                LogException.WriteLogToFile(e.ExceptionObject as Exception, true);
                SockLib.Device.ModbusDevices.ModBusDevice.SaveConfig();
            }
            catch
            {
                //MessageBox.Show("不可恢复的WPF窗体线程异常，应用程序将退出！");
            }
            finally
            {
                //UI线程正在重启动就等待10秒，仍未处理完则结束程序
                if (!IsRestart)
                {
                    IsRestart = true;
                   // ClosePort();
                    RestartMe(e.ExceptionObject as Exception);
                }
                System.Threading.Thread.Sleep(10000);
            }
        }

        /// <summary>
        /// WPF窗体线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                LogException.WriteLogToFile(e.Exception, true);
                SockLib.Device.ModbusDevices.ModBusDevice.SaveConfig();
            }
            catch
            {
                //MessageBox.Show("不可恢复的WPF窗体线程异常，应用程序将退出！");
            }
            finally
            {
                //子线程如果在重启，e.Handled = true;表示UI处理完毕
                if (!IsRestart)
                {
                    IsRestart = true;
                    //ClosePort();
                    RestartMe(e.Exception);
                    e.Handled = true;
                }
                else
                    e.Handled = true;
            }
        }

        static bool  IsRestart=false;

        /// <summary>
        /// 出现崩溃性问题后重启自己
        /// </summary>
        private void RestartMe(Exception  msg)
        {
            //if (Socket服务器.ServerManager.Instance != null)
            //{
            //    Socket服务器.ServerManager.Instance.Dispatcher.Invoke(new Action(() =>
            //    {
            //        RestartWindow RW = new Socket服务器.RestartWindow(msg);
            //        RW.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //        RW.ShowDialog();
            //    }));
            //}
            System.Diagnostics.Process.Start(this.GetType().Assembly.Location);
            System.Environment.Exit(0);
        }

        private void ClosePort()
        {
            Socket服务器.ServerManager.Instance.Dispatcher.Invoke(new Action(() =>
            {
                foreach (SocketManager SManager in Socket服务器.ServerManager.Instance.listbox.Items)
                {
                    SManager.socketserver.StopServer();
                }
            }));
        
        }
    }

   



}
